<?php

if(!defined('IN_INDEX'))
{
	printError("ACCESS DENIED", "This page is hidden.");
	exit;
}

function update_gym_data($gym_id , $array_data){
	global $db;
	$arr = array();
	
	$USER = new User('id',$_SESSION['user_id']);
	
	foreach($array_data as $key=>$val){
		$arr[]= " `$key`='$val' ";
	}
	if(who_am_i()== 'ADMIN' || who_am_i() == 'MOD'){
		$arr[]= " `gym_activated`='1'";
	}
	$gym_info_array = fetch_gym_by_id($gym_id);
	$edit_id_array = explode(',', $gym_info_array['gym_modifier_list_id']);
	
	array_push($edit_id_array, $USER->getId());
	$edit_id_array = array_filter($edit_id_array);
	
	$list_edit_id = implode(',',$edit_id_array);
	$arr[]= " `gym_modifier_list_id`='$list_edit_id'";
	
	$arr[]= " `gym_modified_date`=".time();
	$update = implode(',',$arr);
	
	$sql = "UPDATE ".GYM_TABLE." SET $update WHERE gym_id=$gym_id"; 
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	return true;
}

function update_gym_name_at_training_tbl($gym_id,$gym_name){
	global $db;
	$sql = "UPDATE ".TRAINING_TABLE." SET place='".mysql_real_escape_string($gym_name)."' WHERE gym_id=$gym_id";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	return true;
}

function gym_banner($gym_id, $w= 150, $h= 100){
	$gym_info_array = fetch_gym_by_id($gym_id);
	
	$logo = $gym_info_array['gym_logo'];
	return "./gym_pic/timthumb.php?w=$w&h=$h&zc=1&src=./gym_pic/$logo";
}

function update_gym_logo($logo, $gym_id){
	global $db;
	$USER = new User('id',$_SESSION['user_id']);
	
	$gym_info_array = fetch_gym_by_id($gym_id);
	$edit_id_array = explode(',', $gym_info_array['gym_modifier_list_id']);
	
	array_push($edit_id_array, $USER->getId());
	$edit_id_array = array_filter($edit_id_array);
	
	$list_edit_id = implode(',',$edit_id_array);
	
	@unlink( "./gym_pic/".$gym_info_array['gym_logo'] );
	
	$sql = "UPDATE ".GYM_TABLE." SET gym_logo='$logo', `gym_modifier_list_id`='$list_edit_id',gym_modified_date=".time()." WHERE gym_id=$gym_id";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	return true;
}

function gym_status($gym_id){
	$gym_info_array = fetch_gym_by_id($gym_id);
	if($gym_info_array['gym_activated'] == 1){
		$status = 'ACTIVE';
	}else{
		$status = 'DEACTIVE';
	}
	return $status;
}

function update_gym_status($status, $gym_id){
	global $db;
	
	$active = ($status == 'active') ? 1:0;
	
	$sql = "UPDATE ".GYM_TABLE." SET gym_activated='$active' WHERE gym_id=$gym_id";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	return true;
}

function update_gym( $gym_id, $gym_name){
	global $db;
	$gym_id = intval($gym_id); 
	$try_name_array = fetch_gym_by_name($gym_name);
	
	if($gym_id == 0){
		if(!empty($try_name_array)){ return $try_name_array['gym_id'];}
	}else{
		if($gym_id == $try_name_array['gym_id']){return $gym_id;}
	}
	
	$sql = "INSERT INTO ".GYM_TABLE." SET gym_name='".mysql_real_escape_string($gym_name)."', gym_activated='0',gym_creater_id='".$_SESSION['user_id']."', gym_created_date=".time();
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	
	return mysql_insert_id();
}

function fetch_gym_by_id($gym_id){
	global $db;
	
	$sql = "SELECT * FROM ".GYM_TABLE." WHERE gym_id=".$gym_id."";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	$row = mysql_num_rows($result);
	if($row>0){
		return $db->sql_fetchrow($result);
	}
	return null;
}

function fetch_gym_by_name($gym_name){
	global $db;
	$gym_name = strtolower ($gym_name);
	$sql = "SELECT * FROM ".GYM_TABLE." WHERE LOWER(gym_name) LIKE '".mysql_real_escape_string($gym_name)."'";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	$row = mysql_num_rows($result);
	if($row>0){
		return $db->sql_fetchrow($result);
	}
	return null;
}

function list_users_visitted_this_gym($gym_id){
	global $db;
	
	$sql = "SELECT A.*, COUNT(*) AS total,B.user_firstname,B.user_lastname FROM ".TRAINING_TABLE." AS A LEFT JOIN ".USERS_TABLE." AS B ON A.user_id=B.user_id WHERE A.gym_id=$gym_id GROUP BY (A.user_id)";

	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	$row = mysql_num_rows($result);
	if($row>0){
		$array_return = array();
		while( $row = $db->sql_fetchrow($result) ){
			$array_return[] = $row;
		}
		return $array_return;
	}
	
	return null;
}

function count_total_numbers_visitted($gym_id, $user_id){
	global $db;
	
	$sql = "SELECT * FROM ".TRAINING_TABLE." WHERE gym_id=$gym_id AND user_id=$user_id"; 
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	return mysql_num_rows($result);
}

function count_number_gyms_need_activate(){
	global $db;
	
	$sql = "SELECT A.* FROM ".TRAINING_TABLE." AS A INNER JOIN ".GYM_TABLE." AS B ON A.gym_id=B.gym_id WHERE B.gym_activated !='1' GROUP BY(A.gym_id)";
 
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	return mysql_num_rows($result);
}

function list_all_gyms_activated(){
	global $db;
	
	$sql = "SELECT * FROM ".GYM_TABLE." WHERE gym_activated='1' ";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	
	$array_return = array();
	while( $row = $db->sql_fetchrow($result) ){
		$array_return[] = $row;
	}
	return $array_return;
}

function count_total_members_used($gym_id){
	global $db;
	
	$sql = "SELECT A.* FROM ".TRAINING_TABLE." AS A INNER JOIN ".GYM_TABLE." AS B ON A.gym_id=B.gym_id WHERE B.gym_id =$gym_id GROUP BY(A.user_id)";
 
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	return mysql_num_rows($result);
}
function update_trainning_table($gym_id, $gymTempId, $gym_name){
	global $db;
	if(who_am_i()== 'ADMIN' || who_am_i() == 'MOD'){
		$sql = "UPDATE ".TRAINING_TABLE." SET place='$gym_name', gym_id ='$gymTempId'   WHERE gym_id='$gym_id'";
		if(!$result = $db->sql_query($sql))
		{
			printError("SQL ERROR", mysql_error($sql));
		}
		return true;
	}
	
}
function delete_gym($gym_id){
	global $db;
	if(who_am_i()== 'ADMIN' || who_am_i() == 'MOD'){
		$sql = "DELETE FROM ".GYM_TABLE." WHERE gym_id=$gym_id";
		if(!$result = $db->sql_query($sql))
		{
			printError("SQL ERROR", mysql_error($sql));
		}
		return true;
	}
}

